package com.zx.base.http;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zx.base.util.URLHelper;

public class HttpUtils {

	// private static int timeoutSocket = 10000;

	public interface CallBack {
		public void onSuccess(JSONObject response);

		public void onFailure(Throwable e);
	}

	public static void post(Map<String, String> fields, final CallBack callback) {
		if (fields != null) {
			RequestParams params = new RequestParams(fields);
			AsyHttpClient.get("", params, new JsonHttpResponseHandler() {

				@Override
				public void onSuccess(JSONObject response) {
					callback.onSuccess(response);
				}

				@Override
				public void onFailure(Throwable e, JSONObject errorResponse) {
					callback.onFailure(e);
				}

			});
		}
	}

	// post data whit image
	public static String postImage(Map<String, Object> fields) {
		try {

			HttpClient httpClient = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();

			HttpPost httpPost = new HttpPost(URLHelper.mPrefix + "?act=" + (String) fields.get("act"));
			MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			// CompressFormat set up to JPG, you can change to PNG or whatever
			// you want;
			if (fields.get("photo") instanceof Bitmap) {
				((Bitmap) fields.get("photo")).compress(CompressFormat.JPEG, 60, bos);
			}
			byte[] data = bos.toByteArray();

			// sending a String param;
			for (Map.Entry<String, Object> entry : fields.entrySet()) {
				if (entry.getValue() instanceof String) {
					if (!((String) entry.getValue()).equals("act")) {
						String value = (String) entry.getValue();
						entity.addPart(entry.getKey(), new StringBody(value, Charset.forName(HTTP.UTF_8)));
					}
				}
			}

			// sending a Image;
			// note here, that you can send more than one image, just add
			// another param, same rule to the String;
			entity.addPart("photo", new ByteArrayBody(data, "temp.jpg"));

			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost, localContext);
			BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
			String sResponse = reader.readLine();
			return sResponse;
		} catch (Exception e) {
			Log.v("myApp", "Some error came up");
		}
		return "";
	}

	public static boolean checkConnectivity(ConnectivityManager con) {
		ConnectivityManager connectionManager = con;
		try {
			if (connectionManager.getActiveNetworkInfo().isConnected()) {
				return true;
			} else {
				return false;
			}
		} catch (NullPointerException e) {
			return false;
		}
	}

	public static boolean checkWifi(Activity activitiy) {
		WifiManager mWifiManager = (WifiManager) activitiy.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
		int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
		if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
			// System.out.println("**** WIFI is on");
			return true;
		} else {
			// System.out.println("**** WIFI is off");
			return false;
		}
	}
}
